package com.engine.yhzc.sync.action;

import com.alibaba.fastjson.JSONObject;
import com.engine.addons.common.entity.ResultMsg;
import com.engine.addons.common.util.LogUtil;
import com.engine.addons.ecology.common.entity.RequestInfoBean;
import com.engine.addons.ecology.common.util.RequestInfoUtil;
import com.engine.addons.init.api.service.APIDataInitService;
import com.engine.addons.init.api.service.impl.APIDataInitServiceImpl;
import com.engine.addons.init.dataProcess.entity.DataFormTable;
import com.engine.addons.init.dataProcess.entity.Map4Json;
import com.engine.yhzc.sync.util.FixedAssetsSyncUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FixedAssetsSyncAction implements Action {
    private static final Log log = LogFactory.getLog(FixedAssetsSyncAction.class);
    @Override
    public String execute(RequestInfo requestInfo) {
        try {
            log.info(requestInfo.getRequestid());

            RequestInfoBean rib = new RequestInfoBean(requestInfo);
            Map<String, Object> mainMap = rib.getMainMap();
            log.info("requestInfo:"+ JSONObject.toJSONString(rib));
            List<Map4Json> map4JsonList = new ArrayList<>();
            map4JsonList.add(RequestInfoUtil.requestInfoToMap4Json(rib));

            String  confName = "dto";

            //处理数据
            APIDataInitService as = APIDataInitServiceImpl.getInstance();
            ResultMsg<DataFormTable> apiTableResultMsg = as.processData(null, map4JsonList, confName);

            if (!apiTableResultMsg.isStatus()) {
                requestInfo.getRequestManager().setMessagecontent("固定资产解析出错：" + apiTableResultMsg.getText());
                return Action.FAILURE_AND_CONTINUE;
            }
            log.info("apiTableResultMsg:"+JSONObject.toJSONString(apiTableResultMsg));
            JSONObject sendJson = FixedAssetsSyncUtil.covertTableToJson(apiTableResultMsg.getObject());
            {
                //调用接口
                ResultMsg<String> apiReturnMsg = FixedAssetsSyncUtil.callSync(sendJson.toJSONString());
                if (!apiReturnMsg.isStatus()) {
                    requestInfo.getRequestManager().setMessagecontent("调用固定资产推送接口出错：" + apiReturnMsg.getText());
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            log.info(requestInfo.getRequestid() + "处理固定资产接口成功");
            return Action.SUCCESS;
        } catch (Exception e) {
            log.error("处理固定资产接口出错" + e);
            requestInfo.getRequestManager().setMessagecontent("处理固定资产接口出错" + e.getMessage());
        }

        return Action.FAILURE_AND_CONTINUE;
    }
}

